package com.qixiucoding.write;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.qixiucoding.read.noObjectReadExcel.NoModelDataListener;
import com.qixiucoding.util.TestFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCustomerDataList;

import java.io.File;
import java.util.*;
import java.util.concurrent.RecursiveTask;

/**
 * @Description TODO
 * @Author longhj
 * @Date 2022/11/05 星期六 21:42
 */
@Slf4j
public class NoModelWrite {
    @Test
    public void noModelRead() {
        String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // 这里 只要，然后读取第一个sheet 同步读取会自动finish
        NoModelDataListener noModelDataListener = new NoModelDataListener();
        EasyExcel.read(fileName, noModelDataListener).sheet().doRead();
        //取出的数据key为数字，不符合需要
        List<Map<Integer, Object>> objects = EasyExcel.read(fileName, noModelDataListener).sheet().doReadSync();
        List<Map<String, Object>> resultDataList = noModelDataListener.getResultDataList();
        System.out.println();
    }
    @Test
    public void noModelWriteByMap() throws Exception {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 10; i++) {

            Map<String, Object> mapTmp = new HashMap<>();
            mapTmp.put("A", i + "A");
            mapTmp.put("B", i + "B");
            mapTmp.put("C", i + "C");
            mapTmp.put("D", i + "D");
            list.add(mapTmp);
        }

        String fileName = TestFileUtil.getPath() + "noModelWrite1.xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        writeNoModel(list, fileName);
    }

    public boolean writeNoModel(List<Map<String, Object>> dataList,String path) throws Exception {
        return writeNoModel(dataList, path, "模板");
    }

    public boolean writeNoModel(List<Map<String, Object>> dataList,String path,String sheetName) throws Exception {

        if (CollUtil.isEmpty(dataList)) {
            log.warn("writeNoModel传输数据为空！");
            return false;
        }
        if (StrUtil.isBlank(path)) {
            log.warn("writeNoModel写路径为空！");
            return false;
        }
        if (StrUtil.isBlank(sheetName)) {
            log.warn("writeNoModel参数sheetName为空！");
            return false;
        }
        List<List<String>> head = getHead(dataList);
        List<List<Object>> excelDataList = getDataList(dataList, head);
        try {
            EasyExcel.write(path).head(head).sheet(sheetName).doWrite(excelDataList);
        } catch (Exception e) {
            log.error("writeNoModel方法写Excel文件异常件:{}",e);
            throw e;
        }
        return true;

    }

    private List<List<Object>> getDataList(List<Map<String, Object>> list, List<List<String>> heads) {
        List<List<Object>> dataList = ListUtils.newArrayList();
        if (CollUtil.isNotEmpty(list)) {
            for (Map<String, Object> map : list) {
                List<Object> oneStr = ListUtils.newArrayList();
                for (List<String> head : heads) {
                    Object orDefault = map.getOrDefault(head.get(0), StrUtil.EMPTY);
                    oneStr.add(orDefault);
                }
                dataList.add(oneStr);
            }

        }
        return dataList;
    }

    private List<List<String>> getHead(List<Map<String, Object>> list) {
        List<List<String>> headList = ListUtils.newArrayList();
        if (CollUtil.isNotEmpty(list)) {
            //获取第一个Map，拿表头信息
            Map<String, Object> getHeadMap = list.get(0);
            for (String s : getHeadMap.keySet()) {
                List<String> oneHeadStr = ListUtils.newArrayList();
                oneHeadStr.add(s);
                headList.add(oneHeadStr);
            }

        }
        return headList;

    }

    /**
     * 不创建对象的写
     */
    @Test
    public void noModelWrite() {
        // 写法1
        String fileName = TestFileUtil.getPath() + "noModelWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList());
    }

    private List<List<String>> head() {
        List<List<String>> list = ListUtils.newArrayList();
        List<String> head0 = ListUtils.newArrayList();
        head0.add("字符串" + System.currentTimeMillis());
        List<String> head1 = ListUtils.newArrayList();
        head1.add("数字" + System.currentTimeMillis());
        List<String> head2 = ListUtils.newArrayList();
        head2.add("日期" + System.currentTimeMillis());
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }

    private List<List<Object>> dataList() {
        List<List<Object>> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            List<Object> data = ListUtils.newArrayList();
            data.add("字符串" + i);
            data.add(new Date());
            data.add(0.56);
            list.add(data);
        }
        return list;
    }
}
